/**
 * author:Administrator
 * date:2016/2/24
 * desc:TODO
 */
'use strict';

let seq = 100;
let list = {};
class UserAuth {


  static * genId() {
    return 'user_' + (seq++);
  }

  static * store(obj) {
    if (!obj.id) obj.id = yield UserAuth.genId();
    list[obj.id] = obj;
  }

  static * find(id) {
    return list[id];
  }

  static * findByName(name) {
    let ids = Object.keys(list);
    for (let i = 0; i < ids.length; i++) {
      let item = list[ids[i]];
      if (item.name == name)
        return item;
    }
    return null;
  }

  static * findByNameAndPwd(name, pwd) {
    //console.log('all user ', list);
    //console.log('_userAuth find name=%s,pwd=%s', name, pwd);
    let ids = Object.keys(list);
    for (var id of ids) {
      let item = list[id];
      if (item.name == name && item.pwd == pwd)
        return item;
    }
    return null;
  }

}
module.exports = UserAuth;